package com.ykt.model;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.ykt.common.utils.Colls;
import com.ykt.common.utils.PageUtil;
import com.ykt.common.utils.UB;
import com.ykt.common.utils.page.PageData;
import com.ykt.common.utils.page.PageParam;

import java.util.List;
import java.util.Map;

/**
 * 订单Dao
 *
 * @author wing
 * @version 1.0.0
 */
public class Order extends BaseModel<Order> {
    private static final long serialVersionUID = 1L;

    public static final String order_no = "order_no";
    public static final String order_pno = "order_pno";
    public static final String order_type = "order_type";
    public static final String user_id = "user_id";
    public static final String receipt_name = "receipt_name";
    public static final String receipt_phone = "receipt_phone";
    public static final String shop_id = "shop_id";
    public static final String amount = "amount";
    public static final String pay_amount = "pay_amount";
    public static final String agent_fee = "agent_fee";
    public static final String pay_time = "pay_time";
    public static final String pay_serial_no = "pay_serial_no";
    public static final String status = "status";
    public static final String service_status = "service_status";
    public static final String address = "address";
    public static final String remarks = "remarks";
    public static final String delivery_time = "delivery_time";
    public static final String community_id = "community_id";
    public static final String community_name = "community_name";
    public static final String community_address = "community_address";
    public static final String community_contacts_name = "community_contacts_name";
    public static final String community_contacts_phone = "community_contacts_phone";

    public static final Order dao = new Order();

    // 订单状态
    public enum Status {
        MERCHANT_UNCONFIRMED, //用户已确认->待商户确认
        USER_UNPAYMENT, //商户已确认->待用户支付
        MERCHANT_UNDELIVERY, //用户已支付->待商户发货
        USER_UNRECEIPT, //商户已发货->待用户收货
        HAD_RECEIPT, //用户点击收货->已收货
        AFTER_SALING, //用户点击申请售后->售后中
        COMPLETED, //用户已收货->完成
        CANCEL, //取消
        MERCHANT_RETURN;// 已退款
    }

    // 售后状态
    public enum ServiceStatus {
        DELAY_SERVICE,// 待售后
        COMPLETED, //完成
    }

    // 状态类型
    public enum StatusType {
        STATUS,// 订单状态
        SERVICE_STATUS // 售后状态
    }

    // 订单类型
    public enum OrderType {
        /**单个订单*/
        SINGLE,
        /**父订单*/
        PARENT,
        /**子订单*/
        SON
    }

    @Override
    public Page<Order> paginate(int pageNumber, int rows, Map<String, String> paramMap) {
        String select = "SELECT u.name userName,u.phone userPhone, s.name shopName, "
        		+ "o.* ";
        String exceptSelect = " FROM `order` o left join `user` u on o.user_id = u.id left join `shop` s on o.shop_id=s.id ";

        String sqlOrderBy = " ORDER BY o.id DESC ";

        paramMap.put(OB, sqlOrderBy);

        return this.paginate(select, exceptSelect, pageNumber, rows, paramMap);
    }

    /**
     * 根据user Id 和 order id 查询父订单信息列表
     *
     * @param param 查询参数
     * @return
     */
    public PageData<Order> paginateByUserIdAndStatus(PageParam<Map<String, Object>> param) {
        String select = "SELECT o.*, s.name shopName";
        String from = " FROM `order` o LEFT JOIN `shop` s ON o.shop_id=s.id";
        String where = " WHERE o.order_no=o.order_pno AND o.user_id=?";
        String sort = " ORDER BY o.id DESC ";

        int userId = UB.i(param.getP().get(Order.user_id), -1);
        String status = (String) param.getP().get(Order.status);

        List<Object> array = Colls.array2List((Object) userId);
        if (StrKit.notBlank(status)) {
            where += " AND o.status=?";
            array.add(status);
        }

        from += where + sort;
        Page<Order> page = this.paginate(param.getPageNo(), param.getPageSize(), select, from, array.toArray());

        return PageUtil.to(page);
    }

    /**
     * 根据user Id 和 order id 查询父订单信息列表
     *
     * @param param 查询参数
     * @return
     */
    public PageData<Order> paginateByUserIdAndServiceStatus(PageParam<Map<String, Object>> param) {
        String select = "SELECT o.*, s.name shopName";
        String from = " FROM `order` o LEFT JOIN `shop` s ON o.shop_id=s.id";
        String where = " WHERE o.user_id=?";
        String sort = " ORDER BY o.id DESC ";

        int userId = UB.i(param.getP().get(Order.user_id), -1);
        List<Object> args = Colls.array2List((Object) userId);

        String serviceStatus = (String) param.getP().get(Order.service_status);
        if(StrKit.isBlank(serviceStatus)){
            where += " AND o.service_status IS NOT null";
        }
        if (StrKit.notBlank(serviceStatus)) {
            where += " AND o.service_status=?";
            args.add(serviceStatus);
        }

        from += where + sort;
        Page<Order> page = this.paginate(param.getPageNo(), param.getPageSize(), select, from, args.toArray());

        return PageUtil.to(page);
    }

    /**
     * 根据 订单ID 查询订单详情，不含商品信息
     *
     * @param orderId 订单ID
     * @return
     */
    public Order findById(Integer orderId) {
        String select = "SELECT o.*, s.name shopName, u.name userName";
        String exceptSelect = " FROM `order` o" +
                " LEFT JOIN `shop` s ON o.shop_id=s.id" +
                " LEFT JOIN `user` u ON o.user_id=u.id" +
                " WHERE o.id=?";

        String sql = select + exceptSelect;
        return this.findFirst(sql, orderId);
    }

    public List<Order> findByOrderPno(String orderPno) {
        String select = "SELECT o.*, s.name shopName";
        String from = " FROM `order` o LEFT JOIN `shop` s ON o.shop_id=s.id";
        String where = " WHERE o.order_no!=o.order_pno AND o.order_pno=?";

        return super.find(select + from + where, orderPno);
    }

    public int updateByOrderId(int orderId, String orderStatus) {
        String sql = "UPDATE `order` SET `status`=? WHERE order_pno IN (SELECT o.order_pno FROM ((SELECT order_pno FROM `order` WHERE id=?) o))";
        return Db.update(sql, orderStatus, orderId);
    }

    /**
     * 查询店铺的所有订单概要信息
     *
     * @param param 商铺ID
     * @return 订单概要信息
     */
    public PageData<Order> paginateByShopId(PageParam<Integer> param) {
        String select = "SELECT o.amount, o.pay_amount, o.created_time, u.phone userName";
        String from = " FROM `order` o LEFT JOIN `user` u ON o.user_id=u.id";
        String where = " WHERE o.shop_id=?";
        String sort = " ORDER BY o.id DESC";

        from += where + sort;
        Page<Order> page = this.paginate(param.getPageNo(), param.getPageSize(), select, from, param.getP());

        return PageUtil.to(page);
    }

}
